package com.smyh.UIM.host.state;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.smyh.UIM.CustomDialog;
import com.smyh.UIM.HostFragment;
import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.UIM.host.param.Param_101Activity;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.model.host.state.GroupModel;
import com.smyh.protcol.host.HostCMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 姜龙 on 2016/7/7.
 */
public class GroupActivity extends Fragment {
    public static int groupNum = 1;
    ScrollView ktableView;
    Button read_state_btn;
    RadioGroup radioGroup;
    View view;

    Dialog dialog;
    static boolean isRefresh = false;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        if(view == null){
            view = inflater.inflate(R.layout.activity_host_state_group, container, false);
        }
        initView();
        initEvent();
        return view;
    }


    private void initEvent() {
            read_state_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(MinaClientThread.session != null){
                        if(!HostFragment.connection_btn.isChecked()){
                            Toast.makeText(getContext(), "请建立连接", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(!HostFragment.ready_btn.isChecked()){
                            Toast.makeText(getContext(), "请ready", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        isRefresh = false;
                        dialog = CustomDialog.createLoadingDialog(getContext(),"正在读取数据，请稍后...");
                        byte[] linkAddr = HexStrUtil.hexStr2Bytes(Param_101Activity.param101Map.get(MainCahce.chooseDeviceAddr).getLinkAddr());
                        IoBuffer buf = HostCMDUtil.allCalls(linkAddr);
                        MinaClientThread.session.write(buf);

                        final Long time = System.currentTimeMillis();
                        dialog.show();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (true){
                                    if(System.currentTimeMillis() - time >5000||isRefresh){
                                        dialog.dismiss();
                                        if(!isRefresh) {
                                            handler1.sendMessage(new Message());
                                        }
                                        return;
                                    }
                                    try {
                                        Thread.sleep(300);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }).start();
                    }else{
                        Toast.makeText(getContext(), "没有连接WIFI", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.group_1:
                        group.check(R.id.group_1);
                        groupNum = 1;
                        break;
                    case R.id.group_2:
                        group.check(R.id.group_2);
                        groupNum = 2;
                        break;
                    case R.id.group_3:
                        group.check(R.id.group_3);
                        groupNum = 3;
                        break;
                }
                Toast.makeText(getContext(), "切换组成功", Toast.LENGTH_SHORT).show();
                GroupModel.setTableLayout();
            }
        });
    }
    Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(MainActivity.context, "状态量读取失败...", Toast.LENGTH_SHORT).show();
            isRefresh = false;
        }
    };
    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            GroupModel.setTableLayout();
            Toast.makeText(MainActivity.context, "主机参数读取成功...", Toast.LENGTH_SHORT).show();
            isRefresh = true;
        }
    };
    public static void refreshHostState(){
        handler.sendMessage(new Message());
    }

    private void initView() {
        radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        ktableView = (ScrollView) view.findViewById(R.id.ktable);
        read_state_btn = (Button) view.findViewById(R.id.read_state);
        if(ktableView.getChildCount()== 0) {
            ScrollView parent = (ScrollView) GroupModel.getTableLayout(getContext()).getParent();
            if (parent != null&&parent.getChildCount()>0) {
                parent.removeAllViews();
            }
            ktableView.addView(GroupModel.getTableLayout(getContext()));
        }
    }
}
